<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>  
<meta name="Author" content="zhuoyr">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<style type="text/css">
<!--
 
textarea{
	width: 100%;
	height: 300px;
}
/*****/

/********************/


/*********xxxxxxxxxxxxxxxxxxxxxx***********/
//-->
</style>
</head>

<body>

<textarea id="in">
  HISTORY_ID    NUMBER(12) not null,
  APP_TYPE      NUMBER(4) not null,
  STATUS        NUMBER(2) not null,
  APP_ID        NUMBER(12) not null,
  APP_KEY       VARCHAR2(32) not null,
  APP_NAME      VARCHAR2(64) not null,
  APPLY_TIME    DATE not null,
  DESCRITION    VARCHAR2(256) not null,
  DEVELOPER_ID  NUMBER(12) not null,
  DOWN_TIME     DATE,
  LOGO_FILE_ID  VARCHAR2(20) not null,
  PUBLISH_TIME  DATE,
  MODIFY_TIME   DATE,
  MODIFY_ID     NUMBER(8),
  MODIFY_REASON VARCHAR2(256)
</textarea>
<button id="btn" onclick="map2param();">转param</button>
<button id="btn" onclick="map2bean(false);">转bean</button>
<button id="btn" onclick="map2bean(true);">转bean带注释</button>
<button id="btn" onclick="map2dao();">转dao</button>
<button id="btn" onclick="map2ibatis();">转ibatis</button>
<button id="btn" onclick="map2sql();">转sql</button>
<button id="btn" onclick="map2xml();">转xml</button>
<button id="btn" onclick="map2sb(false);">转StringBuffer</button>
<button id="btn" onclick="map2sb(true);">转StringBuffer(链式)</button>
<textarea   id="out"></textarea>

<script  type="text/javascript">
<!--
var objIn=document.getElementById('in');
var objOut=document.getElementById('out');
String.prototype.toFirstCharUpper=function(){
	if(this.length>1){
		return  this.substr(0,1).toUpperCase()+this.substr(1).toLowerCase();
	}
	return this;
}

String.prototype.toFirstCharLower=function(){
	if(this.length>1){
		return  this.substr(0,1).toLowerCase()+this.substr(1);
	}
	return this;
}

String.prototype.toMethodName =function (){
	var ws=this.split('_');
	var rs='';
	for(var i=0;i<ws.length;i++){
		rs+=ws[i].toFirstCharUpper();
	}
	return rs;
}

String.prototype.toCamelName =function (){
	var ws=this.toLowerCase().split('_');
	var rs='';
	for(var i=0;i<ws.length;i++){
		if(i==0){
			rs+=ws[i]
		}else{
			rs+=ws[i].toFirstCharUpper();
		}
		
	}
	return rs;
}

var fn=function(){console.log(arguments);};

function toJavaTypeFormSql(type){
	type=type.toLowerCase();
	//console.log(type);
	if(/^(var|text|char)/.test(type)){
		return 'String';
	}else if( /^date/.test(type) ){
		//return 'Date';
		return 'String';
	}else if( /^float/.test(type) ){
		//return 'Date';
		return 'Float';
	}else if( /^num/.test(type) ||  /^int/.test(type) ){
		var result='Object';
		var res=type.match(/(\d+)/ig);
		if(res){
			if(res.length==1){
				//if(parseInt(res[0])<8){
					//result='int';
				//}else{
					//result='long';
				//}
				result='int';
			}else if(res.length==2){
				if(parseInt(res[1])<8){
					result='float';
				}else{
					result='double';
				}
			}
		}
		  
		 return result;
	}else if( /^clob/.test(type) ){
		//return 'Date';
		return 'String';
	}
}

function map2bean( isComment ){
	txt=objIn.value;
	var aData=txt.split(/\n/);
	var aaData=[];
	
	for(var i=0;i<aData.length;i++){
		if(aData[i].length>2)
		aaData.push( aData[i].replace(/(^\s+|\s+$)/,'').split(/\s+/) );
	}
	var rsField='';
	var rsMethod='';
	//console.log(aaData);
	for(var i=0;i<aaData.length;i++){
		aData=aaData[i];
		var field_type=toJavaTypeFormSql(aData[1]);
		var field_name=aData[0].toLowerCase();
		//var field_name=aData[0].toCamelName();
		var method_name=aData[0].toMethodName();
		var setter='public void set'+method_name+'('
			+field_type+' '+field_name+'){ this.'+field_name+' =  '+field_name+';}'+"\n";
		var getter='public '+field_type+' get'+method_name+'( ){  return this.'+field_name+';}'+"\n";
		if(isComment){
			rsField+= '/*'+aData[2]+"*/\n";
		}
		rsField+= 'private '+field_type+' '+field_name+";\n";
		rsMethod+=setter+getter;
		
	}

	objOut.value=rsField+"\n\n"+rsMethod;
	

 
}

function map2dao(  ){
	txt=objIn.value;
	var aData=txt.split(/\n/);
	var aaData=[];
	
	for(var i=0;i<aData.length;i++){
		if(aData[i].length>2)
		aaData.push( aData[i].replace(/(^\s+|\s+$)/,'').split(/\s+/) );
	}
	var rsParam='';
	var rsResult='';
	//console.log(aaData);
	for(var i=0;i<aaData.length;i++){
		aData=aaData[i];
		var field_type=toJavaTypeFormSql(aData[1]);
		var field_name=aData[0].toLowerCase();
		var method_name=aData[0].toMethodName();
		//var setter='public void set'+method_name+'('
			//+field_type+' '+field_name+'){ this.'+field_name+' =  '+field_name+';}'+"\n";
		//var getter='public '+field_type+' get'+method_name+'( ){  return this.'+field_name+';}'+"\n";

		var params='param.setParamByName(req.get'+method_name+'(), DBParam.'+field_type.toUpperCase()+'_TYPE, "'+field_name.toUpperCase()+'");';
		var result='bean.set'+method_name+'('+resultConvert(field_type,i)+');';

		//rsField+= 'private '+field_type+' '+field_name+";\n";
		//rsMethod+=setter+getter;
		rsParam+= params+"\n";
		rsResult+=result+"\n";
		
	}

	objOut.value=rsParam+"\n\n"+rsResult;
	

 
}


function map2param(  ){
	txt=objIn.value.replace(/`/ig,'');
	var aData=txt.split(/\n/);
	var aaData=[];
	
	for(var i=0;i<aData.length;i++){
		if(aData[i].length>2)
		aaData.push( aData[i].replace(/(^\s+|\s+$)/,'').split(/\s+/) );
	}
	var rsParam='';
	var rsResult='';
	
	for(var i=0;i<aaData.length;i++){
		aData=aaData[i];
		var field_type=toJavaTypeFormSql(aData[1]);
		var field_name=aData[0].toLowerCase();
		var method_name=aData[0].toMethodName();
        var result=''+field_type+' '+field_name+'=HttpParameter.get'+field_type.toMethodName()+'Parameter(request, "'+field_name+'");';
		var params='param.setParamByName('+field_name+', DbParam.'+field_type.toUpperCase()+'_TYPE, "'+field_name.toUpperCase()+'");';

		rsParam+= params+"\n";
		rsResult+=result+"\n";
		
	}

	objOut.value=rsResult+"\n\n"+rsParam;
	

 
}

function resultConvert(field_type,i){
	if('String'==field_type){
		return 'result.aaRecord[i]['+i+']';
	}else if('Date'==field_type){
		return 'new Date(result.aaRecord[i]['+i+'])';
	}else if('int'==field_type){
		return 'Integer.getInteger(result.aaRecord[i]['+i+'])';
	}else if('long'==field_type){
		return 'Long.parseLong(result.aaRecord[i]['+i+'])';
	}else if('float'==field_type){
		return 'Float.parseFloat(result.aaRecord[i]['+i+'])';
	}else if('double'==field_type){
		return 'Double.parseDouble(result.aaRecord[i]['+i+'])';
	}
}


function map2ibatis(  ){
	txt=objIn.value;
	var aData=txt.split(/\n/);
	var aaData=[];
	
	for(var i=0;i<aData.length;i++){
		if(aData[i].length>2)
		aaData.push( aData[i].replace(/(^\s+|\s+$)/,'').split(/\s+/) );
	}
	 
	var rsResult='';
	var aUpadate=[];
	var aInsertKey=[];
	var aInsertVal=[];
	var aWhere=[];
	//console.log(aaData);
	for(var i=0;i<aaData.length;i++){
		aData=aaData[i];
		var field_type=toJavaTypeFormSql(aData[1]);
		var column_name=aData[0];
		var method_name=column_name.toMethodName();
		var property_name=method_name.toFirstCharLower();
		 

		var result='<result property="'+property_name+'" column="'+column_name+'" />';
	 
		
		aUpadate.push(' '+column_name+'=#'+property_name+'# ');
		aInsertKey.push(column_name);
		aInsertVal.push('#'+property_name+'#');
		aWhere.push('<isNotEmpty prepend="and" property="'+property_name+'">'+"\n t."+column_name+'=#'+property_name+"#\n  </isNotEmpty>");
		rsResult+=result+"\n";
		
	}

	var rsUpdate='update OPEN_DEVELOPER_APPLICATION set '+aUpadate.join(',')+' where ID=#id#';
	var rsInsert='insert into OPEN_DEVELOPER_APPLICATION ('+aInsertKey.join(',')+')values('+aInsertVal.join(',')+')';
	var rsSelect='select '+aInsertKey.join(',')+' from OPEN_DEVELOPER_APPLICATION ';
	var rsFilter=" \n"+aUpadate.join(",\n");
	var rsWhere2=aWhere.join("\n");

	objOut.value=rsResult+"\n\n"+rsUpdate+"\n\n"+rsInsert+"\n\n"+rsSelect+"\n\n"+rsFilter+"\n\n"+rsWhere2;
	

 
}

function prepareSql(){
    var	txt=objIn.value.replace(/`/ig,'');
	var table={name:'TABLE_NAME',dml:txt}

	var exp=/CREATE\s+TABLE\s+(.+)/ig;
	var mat=exp.exec(table.dml);
	if( mat !== null){
		table.name=mat[1];
		table.dml=table.dml.replace(exp,'');
	}
	var cols=[];
	var aData=table.dml.split(/\n/);
	for(var i=0;i<aData.length;i++){
		if(aData[i].length>2)
		cols.push( aData[i].replace(/(^\s+|\s+$)/,'').split(/\s+/) );
	}

	table.cols=cols;
	return table;
}

function map2sql(  ){
	var  table=prepareSql();
	var aaData=table.cols;

 
	 
	var aUpadate=[];
	var aUpadate2=[];
	var aInsertKey=[];
	var aSelect=[];
	var aInsertVal=[];
	 
	for(var i=0;i<aaData.length;i++){
		var aData=aaData[i];
		var field_type=toJavaTypeFormSql(aData[1]);
		var column_type=aData[1];
		var column_name=aData[0];
		var method_name=column_name.toMethodName();
		var property_name=method_name.toFirstCharLower();
		 
		var vtoken='?'
		if(/DATE/ig.test(column_type)){
			vtoken = "TO_DATE(?,'YYYY/MM/dd HH24:MI:SS')" ;
		} 
		aUpadate.push(' '+column_name+'='+vtoken+' ');
		aUpadate2.push(' '+column_name+'=:'+column_name+' ');
		aInsertKey.push(column_name);
		aSelect.push('a.'+column_name);
		aInsertVal.push(':'+column_name);
		
		
		
	}

	var rsUpdate='update '+table.name+' set '+aUpadate.join(',')+' where ID=:ID';
	var rsUpdate2='update '+table.name+' set '+aUpadate2.join(',')+' where ID=:ID';
	var rsInsert='insert into '+table.name+' ('+aInsertKey.join(',')+')values('+aInsertVal.join(',')+')';
	var rsSelect='select '+aSelect.join(',')+' from '+table.name+' a ';

	objOut.value=rsUpdate+"\n\n"+rsUpdate2+"\n\n"+rsInsert+"\n\n"+rsSelect+"\n\n";
	

 
}

function getDbTypeDefault(type){
	if(/^var/.test(type)){
		return 'str';
	}else if( /^date/.test(type) ){
		return '2014/08/29 12:12:12';
	}else if( /^num/.test(type) ){
		  
		 return 1;
	}else if( /^clob/.test(type) ){
		//return 'Date';
		return 'clob';
	}
}

function map2xml(  ){
	txt=objIn.value;
	var aData=txt.split(/\n/);
	var aaData=[];
	
	for(var i=0;i<aData.length;i++){
		if(aData[i].length>2)
		aaData.push( aData[i].replace(/(^\s+|\s+$)/,'').split(/\s+/) );
	}
	 
	var rsResult=['<optype>0</optype>'];
	//console.log(aaData);
	for(var i=0;i<aaData.length;i++){
		aData=aaData[i];
		var column_type=aData[1].toLowerCase();
		var column_name=aData[0].toLowerCase();
		
		 
		 rsResult.push('<'+column_name+'>'+getDbTypeDefault(column_type)+'</'+column_name+'>');

 
		
	}

	 

	objOut.value=rsResult.join("\n");
	
}
function map2sb( isChain ){
	txt=objIn.value;
	var aData=txt.split(/\n/);
	var rsResult=[];

	var prefix=suffix='';
	if(!isChain){
		prefix='sbSql';
		suffix=";\n";
	}else{
		suffix="\n";
	}
	for(var i=0;i<aData.length;i++){
		var line=aData[i].replace(/(^\s+|\s+$)/,'');

		if(line.length>2)
		rsResult.push(prefix+'.append(" '+line+' ")'+suffix );
	}
	 

	objOut.value=rsResult.join("");
}
//-->
</script>
</body>
</html>